home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / win_u_z / wt_jan92.zip / SHAW.ZIP / WINAPP.H < prev   
C/C++ Source or Header  |  1991-11-04  |  11KB  |  340 lines

  1. // winapp.h
  2.  
  3. #if !defined(WINAPP_H)
  4. #define WINAPP_H
  5.  
  6. #include<windows.h>
  7. #include<dos.h>
  8.  
  9. extern void *pWindow;
  10.  
  11. const unsigned WINAPP_RESERVED = sizeof(void *);
  12. inline void *GetPointer(HWND hWnd)
  13.     {
  14. #if defined(__SMALL__) || defined(__MEDIUM__)
  15.     return (void *)GetWindowWord(hWnd,0);
  16. #elif defined(__LARGE__) || defined(__COMPACT__) || defined(__HUGE__)
  17.     return (void *)GetWindowLong(hWnd,0);
  18. #else
  19.     #error Must use Small, Medium, Large, Compact or Huge models!
  20. #endif
  21.     }
  22.  
  23. inline void SetPointer(HWND hWnd, void *ptr)
  24.     {
  25. #if defined(__SMALL__) || defined(__MEDIUM__)
  26.     SetWindowWord(hWnd,0,(WORD)ptr);
  27. #elif defined(__LARGE__) || defined(__COMPACT__) || defined(__HUGE__)
  28.     SetWindowLong(hWnd,0,(LONG)ptr);
  29. #else
  30.     #error Must use Small, Medium, Large, Compact or Huge models!
  31. #endif
  32.     }
  33.  
  34. /* WINAPPLICATION:
  35. An object of this class is defined once for every application. It contains the
  36. items passed to WinMain, as well as access functions for returning them. The
  37. constructor initializes them.
  38. */
  39. extern HANDLE _hInstance, _hPrev;
  40. extern int _cmdShow;
  41. extern unsigned int _psp, _pszCmdline;
  42.  
  43. class WinApplication    // Windows application class
  44.     {
  45.         static HANDLE ApphInstance;
  46.         static HANDLE hPrevInstance;
  47.         static LPSTR lpszCmdLine;
  48.         static int nCmdShow;
  49.     public:
  50.         WinApplication(void)
  51.             {
  52.             ApphInstance = _hInstance;
  53.             hPrevInstance = _hPrev;
  54.             lpszCmdLine = (LPSTR)MK_FP(_psp,_pszCmdline);;
  55.             nCmdShow = _cmdShow;
  56.             }
  57.         static HANDLE GetInstance(void) { return ApphInstance; }
  58.         static HANDLE GetPrevInstance(void) { return hPrevInstance; }
  59.         static LPSTR GetCmdLine(void) { return lpszCmdLine; }
  60.         static int GetCmdShow(void) { return nCmdShow; }
  61.         static int Run(void);    // default message loop processing
  62.    };
  63.  
  64.    
  65. /* WINCLASS
  66. */
  67. const NOSTYLE = 0;
  68. const NOEXTRABYTES = 0;
  69.  
  70. class WinClass : WNDCLASS
  71.     {
  72.         BOOL    class_registered;
  73.     public:
  74.         WinClass(void)
  75.             {
  76.             style = NOSTYLE;
  77.             lpfnWndProc = DefWindowProc;
  78.             cbClsExtra = NOEXTRABYTES;
  79.             cbWndExtra = NOEXTRABYTES;
  80.             hInstance = NULL;
  81.             hIcon = LoadIcon(NULL, IDI_APPLICATION);
  82.             hCursor = LoadCursor(NULL, IDC_ARROW);
  83.             hbrBackground = GetStockObject(WHITE_BRUSH);
  84.             lpszMenuName = NULL;
  85.             lpszClassName = NULL;
  86.             class_registered = FALSE;
  87.             }
  88.             // since 'this' is derived from WNDCLASS, it's passed as one
  89.         void ClassRegister(void)
  90.             {
  91.             RegisterClass(this);
  92.             class_registered = TRUE;
  93.             }
  94.         BOOL ClassRegistered(void)   
  95.             { 
  96.             if(class_registered)
  97.                 return TRUE;
  98.             if(!lpszClassName || !hInstance)
  99.                 return FALSE;
  100.  
  101.             WNDCLASS wndclass;
  102.             
  103.             if(GetClassInfo(hInstance,lpszClassName,&wndclass))
  104.                 {
  105.                 LPSTR menuName = lpszMenuName;
  106.                 HANDLE hInst = hInstance;
  107.                 LPSTR className = lpszClassName;
  108.                 WNDCLASS *wc = this;
  109.                 *wc = wndclass;         // structure assignment
  110.                 lpszClassName = className;
  111.                 lpszMenuName = menuName;
  112.                 hInstance = hInst;
  113.                 return class_registered = TRUE;
  114.                 }
  115.             return FALSE;
  116.             }
  117.     };
  118.  
  119. /* WINHANDLE
  120. An object of this class is created with every instance of Window (below). It
  121. contains data that controls Window creation handling, as well as the window
  122. handle.
  123.  */
  124.  
  125. class WinHandle
  126.     {
  127.         HWND hWnd;
  128.         LPSTR classname;
  129.         LPSTR windowname;
  130.         DWORD winstyle;
  131.         int upper_left_x;
  132.         int upper_left_y;
  133.         int winwidth;
  134.         int winheight;
  135.         HWND winParent;
  136.         HMENU menu;
  137.         HANDLE hInstance;
  138.         LPSTR lpParam;
  139.     public:
  140.         WinHandle(void)
  141.             {
  142.             hWnd = NULL;
  143.             classname = (LPSTR)"WinApplication";
  144.             windowname = (LPSTR)"WinApplication:Window";
  145.             winstyle = 0;
  146.             upper_left_x = upper_left_y = winwidth = winheight = 0;
  147.             winParent = NULL;
  148.             menu = NULL;
  149.             hInstance = 0;
  150.             lpParam = NULL;
  151.             }
  152.  
  153.         BOOL Create(void)
  154.             {
  155.             if(hWnd)    // if window's already created, return TRUE
  156.                 return TRUE;
  157.  
  158.             hWnd = CreateWindow(classname,windowname,winstyle,upper_left_x,
  159.                 upper_left_y,winwidth,winheight,winParent,menu,hInstance,
  160.                 lpParam);
  161.  
  162.             return (hWnd ? TRUE : FALSE);
  163.             }
  164.  
  165.     friend class Window;    // allow Window to modify members
  166.     };
  167.  
  168.  
  169. /* WINDOW
  170.  
  171. Every window is derived from this abstract class.
  172.  
  173. Every class derived from this one must have a WinClass object (or a pointer
  174. to one created by a derived class) and pass it back to Window.
  175.  
  176. And each must supply its own WndProc for the class.
  177. */
  178. class Window : public WinApplication, WinClass
  179.     {
  180.     public:
  181.         HWND WHandle;
  182.     private:
  183.         static long far pascal WndProc(HWND, WORD, WORD, LONG);
  184.         static long far pascal MDIChildWndProc(HWND, WORD, WORD, LONG);
  185.  
  186.         WinHandle WHdl;
  187.         int     current_display;
  188.         int     previously_visible;
  189.         long (far pascal *UserWndProc)(HWND, WORD, WORD, LONG);
  190.  
  191.         void    Show(void)
  192.             { previously_visible = ShowWindow(WHandle,current_display); }
  193.         void SetClassWinXbytes(int xtrabytes)    
  194.             { cbWndExtra = xtrabytes; }
  195.  
  196.     public:
  197.         Window(char *winname)
  198.             {
  199.             SetClassInstance();         // insert instance handle
  200.             SetWinInstance(GetInstance());
  201.             SetClassName(winname);
  202.             SetWinName(winname);
  203.             DefaultDisplay();
  204.             previously_visible = FALSE;
  205.             WHandle = 0;
  206.             SetClassWinXbytes(WINAPP_RESERVED);
  207.             lpfnWndProc = Window::WndProc;
  208.             };
  209.  
  210.         ~Window(void)
  211.             {
  212.             if(WHandle)
  213.                 DestroyWindow(WHandle);
  214.             }
  215.  
  216.             // displays window and creates if not already created
  217.         BOOL Display(void)
  218.             {
  219.             Register();
  220.             if(!Create())
  221.                 return FALSE;
  222.             Show();
  223.             Update();
  224.             return TRUE;
  225.             }
  226.  
  227.         BOOL Display(int display_style)
  228.             {
  229.             current_display = display_style;
  230.             return Display();
  231.             }
  232.  
  233.         BOOL ClassRegistered(void)
  234.             {
  235.             if(WinClass::ClassRegistered())
  236.                 {
  237.                 if(!UserWndProc)
  238.                     SetClassWinProc(lpfnWndProc);
  239.                 return TRUE;
  240.                 }
  241.             return FALSE;
  242.             }
  243.             // should call SetClassInstance and SetClassName if not called
  244.  
  245.         void RegisterMDIChild(void)
  246.             {
  247.             lpfnWndProc = Window::MDIChildWndProc;
  248.             Register();
  249.             }
  250.                 
  251.         void Register(void)
  252.             {
  253.             if(!ClassRegistered())         // if class not registered
  254.                 {
  255.                 if(!GetClassName())         // if class name not set
  256.                     SetClassName("WinApplication");
  257.                 ClassRegister();           // register the class
  258.                 }
  259.             }
  260.  
  261.         BOOL Create(void)
  262.             {
  263.             Register();
  264.             WHdl.hInstance = GetInstance();
  265.             WHdl.classname = lpszClassName;
  266.             WHdl.lpParam = (LPSTR)this;
  267.             return WHdl.Create();
  268.             }
  269.  
  270.         HWND GetHandle(void) { return WHandle; }
  271.  
  272.         void Hide(void)  // hides the window
  273.             { Display(SW_HIDE); }
  274.         void Minimize(void)  // minimizes the window
  275.             { Display(SW_MINIMIZE); }
  276.         void Maximize(void)  // maximizes the window
  277.             { Display(SW_SHOWMAXIMIZED); }
  278.         void Normalize(void) // displays window in original size and position
  279.             { Display(SW_SHOWNORMAL); }
  280.         void DefaultDisplay(void)
  281.             { current_display = GetCmdShow(); }
  282.  
  283.         void Paint(void)     // paints window
  284.             {
  285.             PAINTSTRUCT ps;
  286.  
  287.             BeginPaint(WHandle, &ps);
  288.             EndPaint(WHandle, &ps);
  289.             }
  290.  
  291.         void Update(void)   // updates window
  292.             { UpdateWindow(WHandle); }
  293.  
  294.              // vocab of functions for modifying the registration info
  295.         void SetClassInstance(void)              
  296.             { hInstance = GetInstance(); }
  297.         void SetClassName(LPSTR classname)       
  298.             { lpszClassName = classname; }
  299.         void SetClassStyle(unsigned newstyle)    { style = newstyle;  }
  300.         void AddClassStyle(unsigned addstyle)    { style |= addstyle; }
  301.         void SetClassWinProc(long (FAR PASCAL *lpfnWndProc)(HWND, unsigned, 
  302.             WORD, LONG))
  303.             {   UserWndProc = lpfnWndProc;  }
  304.  
  305.         void AddClassWinXbytes(int xtrabytes)
  306.             { cbWndExtra += xtrabytes;  }
  307.  
  308.         void SetClassIcon(LPSTR iconname)
  309.             {
  310.             hIcon = LoadIcon(GetInstance(),iconname);
  311. //            if(hInstance)
  312. //                hIcon = LoadIcon(hInstance,iconname);
  313.     
  314.             }
  315.         void SetClassBackGround(HBRUSH handle)
  316.             {   hbrBackground = handle;  }
  317.  
  318.         LPSTR GetClassName(void)    { return lpszClassName; }
  319.         void SetClassMenu(LPSTR menuname)   {   lpszMenuName = menuname; }
  320.  
  321.         // vocab of functions for modifying the create info
  322.         void SetWinClassName(LPSTR classname)   {WHdl.classname = classname; }
  323.         void SetWinName(LPSTR winname)  { WHdl.windowname = winname;}
  324.         LPSTR GetWinName(void)          { return WHdl.windowname;}
  325.         void SetWinStyle(DWORD dword)   { WHdl.winstyle = dword;   }
  326.         void AddWinStyle(DWORD dword)   { WHdl.winstyle |= dword;  }
  327.         void SetWinX(int x)             { WHdl.upper_left_x = x;   }
  328.         void SetWinY(int y)             { WHdl.upper_left_y = y;   }
  329.         void SetWinWidth(int width)     { WHdl.winwidth = width;   }
  330.         void SetWinHeight(int height)   { WHdl.winheight = height; }
  331.         void SetWinParent(HWND hWnd)    { WHdl.winParent = hWnd;   }
  332.         void SetWinMenu(HMENU hmenu)    { WHdl.menu = hmenu;       }
  333.         void SetWinInstance(HANDLE hinst){ WHdl.hInstance = hinst; }
  334.         void SetWinParams(LPSTR param)  { WHdl.lpParam = param;    }
  335.     };
  336. #endif
  337.  
  338.  
  339.  
  340.